package web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import web.misc.JsonHttpResponseWriter;

import com.fasterxml.jackson.databind.ObjectMapper;

import data.dao.DataFacade;
import email.EmailFacade;

public class ContainerInitListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent event) {
        ((DataFacade) event.getServletContext().getAttribute("dataLayer")).shutdown();
    }

    public void contextInitialized(ServletContextEvent event) {
        event.getServletContext().setAttribute("emailLayer", new EmailFacade("mail.properties"));

        final ObjectMapper jsonParser = new ObjectMapper();
        event.getServletContext().setAttribute("jsonHandler", new JsonHttpResponseWriter(jsonParser));

        event.getServletContext().setAttribute("dataLayer", new DataFacade("couchdb.properties", jsonParser));
    }
}
